from soj.builtin.compile.cpp.cpp import CppCompile
from soj.builtin.compile.common import *
from soj.common.lang import *
from typing import Any
class CompileOrchestrator:
    def __init__(self,lang:Language,source:Path,setting:Any|None=None):
        self.lang=lang
        print(f"Source is {source.resolve()}")
        ensure_file(source)
        self.source=source
        if lang==Language.CPP:
            self.settings=CPP_DEAFULT if not setting else setting
        elif lang==Language.PYTHON:
            self.settings=PYTHON_DEFAULT
        else:
            raise NotImplementedError("Not supported language")
    def _cpp_impl(self)->CompileResult:
        if self.lang!=Language.CPP: raise RuntimeError("Not a cpp compile task")
        cpp=CppCompile(self.source,self.settings['lang'].value,self.settings["with_o2"])
        return cpp.compile()
    def _py_impl(self)->CompileResult:
        if self.lang!=Language.PYTHON: raise RuntimeError("Not a python compile task")
        ws=WorkSpace(create_files=False)
        ws.import_file(ws.FileType.CUSTOM,self.source,rename="source.py")
        return {
            'artifact':ws.join("source.py"),
            'output':ws,
            'msg':"Python is no need to be compiled",
            'ok':True
        }
    def compile(self):
        if self.lang==Language.CPP:
            return self._cpp_impl()
        elif self.lang==Language.PYTHON:
            return self._py_impl()
        else:
            raise NotImplementedError("Not supported language")